<?php
/**
 * String
 *
 * TO DOC
 *
 * @author  Badrus Said <badrus.said@gmail.com>
 * @protected
 * @singleton
 */
final class api_String extends Wrapper {
    public function __construct() {
        $this->setPrefix("str");
        $this->setLookup(array(
            "length" => "len"
        ));
    }
    private $chars = array(
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G",
        "h", "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S",
        "t", "T", "u", "U", "v", "V", "w", "W", "x", "X", "y", "Y", "z", "Z"
    );
    public function random($j = 8) {
        $randt = $_char = STR_EMPTY;
        $first = true;
        srand((double)microtime() * 1000000);
        for($i = 0, $k = count($this->chars) - 1; $i < $j; $i++) {
            $_char = $this->chars[rand(0, $k)];
            if ($first) {
                if (!ctype_alpha($_char)) continue;
                $first = false;
            }
            $randt.= $_char;
        }
        return $randt;
    }
    public function mix($j = 8, array $name) {
        $first = true;
        srand((double)microtime() * 1000000);
        for($i = 0, $k = sizeof($this->chars) - 1; $i < $j; $i++) {
            $char = $this->chars[rand(0, $k)];
            if ($first) {
                if (!ctype_alpha($char)) continue;
                $first = false;
            }
            if (in_array($char, $name)) continue;
            $name[] = $char;
        }
        shuffle($name);
        $out = implode("", $name);
        return $out;
    }
    public function byteFormat($i, $d = 0) {
        $j = array("TB" => 1099511627776, "GB" => 1073741824, "MB" => 1048576, "kB" => 1024, "B " => 1);
        $b = intval($i);
        foreach($j as $k => $v) {
            if ($b >= $v) return number_format($i / $v, $d) . " " . $k;
        }
        return 0;
    }
    private $e;
    private static $a = "/^[+-]?(\d+)/";
    private static $b = "/\d{1,3}$/";
    private static $c = "/(\d{1,3})$/";
    private static $d = "/\.(\d+)/";
    private function toEN() {
        $a = array(0 => "zero", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine");
        $b = array(0 => STR_EMPTY, 1 => "teen", 2 => "hundred", 3 => "thousand", 6 => "million", 9 => "billion", 12 => "trilion", 15 => "million-billion", 18 => "billion-billion", 21 => "billion-trilion", 24 => "trilion-trilion");
        $n = $m = null;
        $c = 0;
        $d = null;
        if (preg_match(self::$a, $this->e, $m)) $n = $m[1];
        while(preg_match(self::$c, $n, $m)) {
            $e = ($m[1] % 10);
            $f = ($m[1] % 100 - $e) / 10;
            $g = ($m[1] - $f * 10 - $e) / 100;
            $h = ($e == 0) ? STR_EMPTY : $a[$e];
            $k = ($g == 0) ? STR_EMPTY : $a[$g] . " " . $b[2];
            if ($f == 0) {
                if ($e == 0) {} else {
                    if ($e == 1) {
                        $k = ($k ? $k . " " . $a[$e] : ($c == 0 ? $a[1] : "one "));
                    } else {
                        $k = ($k ? $k . " " : STR_EMPTY) . $a[$e];
                    }
                }
            } elseif ($f == 1) {
                if ($e == 0) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "ten";
                } elseif ($e == 1) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "eleven";
                } elseif ($e == 2) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "twelve";
                } elseif ($e == 3) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "thirteen";
                } elseif ($e == 5) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "fifteen";
                } else {
                    $k = ($k ? $k . " " : STR_EMPTY) . $a[$e] . " teen";
                }
            } elseif ($f == 2) {
                $k = ($g == 0) ? ("twenty " . ($k ? $k . " " : STR_EMPTY) . $h) : (($k ? $k . " " : STR_EMPTY) . "twenty " . $h);
            } elseif ($f == 3) {
                $k = ($g == 0) ? ("thirty " . ($k ? $k . " " : STR_EMPTY) . $h) : (($k ? $k . " " : STR_EMPTY) . "thirty " . $h);
            } elseif ($f == 5) {
                $k = ($g == 0) ? ("fifty " . ($k ? $k . " " : STR_EMPTY) . $h) : (($k ? $k . " " : STR_EMPTY) . "fifty " . $h);
            } elseif ($f == 8) {
                $k = ($g == 0) ? ("eighty " . ($k ? $k . " " : STR_EMPTY) . $h) : (($k ? $k . " " : STR_EMPTY) . "eighty " . $h);
            } else {
				$k = ($k? $k . " " : STR_EMPTY) . $a[$f]. "ty" . ($e > 0 ? " " . $a[$e] : STR_EMPTY);
            }
			$d = ($k ? $k . (($k == "one") ? STR_EMPTY : " ") . $b[$c] : STR_EMPTY) . ($d ? " " . $d : STR_EMPTY);
            $n = preg_replace(self::$b, STR_EMPTY, $n);
            $c+= 3;
        }
        if (!$d) $d = $a[0];
        $o = (preg_match(self::$d, $this->e, $m)) ? $m[1] : STR_EMPTY;
        $p = STR_EMPTY;
        $q = strlen($o);
        for($i = 0; $i < $q; $i++) {
            $p.= ($p ? " " : STR_EMPTY) . $a[substr($o, $i, 1)];
        }
        return ((substr($this->e, 0, 1) == "-") ? "minus " : STR_EMPTY) . $d . ($p ? " comma " . $p : STR_EMPTY);
    }
    private function toID() {
        $a = array(0 => "nol", 1 => "satu", 2 => "dua", 3 => "tiga", 4 => "empat", 5 => "lima", 6 => "enam", 7 => "tujuh", 8 => "delapan", 9 => "sembilan");
        $b = array(0 => STR_EMPTY, 1 => "puluh", 2 => "ratus", 3 => "ribu", 6 => "juta", 9 => "miliar", 12 => "triliun", 15 => "juta-miliar", 18 => "juta-triliun", 21 => "miliar-triliun", 24 => "triliun-triliun");
        $n = $m = null;
        $c = 0;
        $d = null;
        if (preg_match(self::$a, $this->e, $m)) $n = $m[1];
        while(preg_match(self::$c, $n, $m)) {
            $e = ($m[1] % 10);
            $f = ($m[1] % 100 - $e) / 10;
            $g = ($m[1] - $f * 10 - $e) / 100;
            $k = ($g == 0) ? STR_EMPTY : (($g == 1) ? ("se" . $b[2]) : ($a[$g] . " " . $b[2]));
            if ($f == 0) {
                if ($e == 0) {} else {
                    $k = ($e == 1) ? (($k ? $k." " . $a[$e] : (( (($c % 3) == 0) && ($c == 3)) ? "se" : $a[1]))) : (($k ? $k." " : STR_EMPTY) . $a[$e]);
                }
            } elseif ($f == 1) {
                if ($e == 0) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "se" . $b[1];
                } elseif ($e == 1) {
                    $k = ($k ? $k . " " : STR_EMPTY) . "sebelas";
                } else {
                    $k = ($k ? $k . " " : STR_EMPTY) . $a[$e] . " belas";
                }
            } else {
                $k = ($k ? $k . " " : STR_EMPTY) . $a[$f] . " puluh" . ($e > 0 ? " " . $a[$e] : STR_EMPTY);
            }
            $d = ($k ? $k . (($k == "se") ? STR_EMPTY : " ") . $b[$c] : STR_EMPTY) . ($d ? " " . $d : STR_EMPTY);
            $n = preg_replace(self::$b, STR_EMPTY, $n);
            $c+= 3;
        }
        if (!$d) $d = $a[0];
        $o = (preg_match(self::$d, $this->e, $m)) ? $m[1] : STR_EMPTY;
        $p = STR_EMPTY;
        $q = strlen($o);
        for($i = 0; $i < $q; $i++) {
            $p.= ($p ? " " : STR_EMPTY) . $a[substr($o, $i, 1)];
        }
        return ((substr($this->e, 0, 1) == "-") ? "minus " : STR_EMPTY) . $d . ($p ? " koma " . $p : STR_EMPTY);
    }
    public function currency($e = "0", $l = "EN") {
        $this->e = $e;
        $m = "to".$l;
        return $this->$m();
    }
}
?>